package com.example.datajpatable.controller;

import com.example.datajpatable.domain.Student;
import com.example.datajpatable.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

//    @RequestMapping("/student/list")
//    public String queryStudent(String name,Model model,
//                               @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
//                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
//        System.out.print("name is:" + name);
////        System.out.print("name1 is:" + name1);
//        List<Student> students = studentService.findBySname(name+"%");
////        model.addAttribute("data",students);
//        if(students.size()!=0){
//            model.addAttribute("data",students);
//        }else{
//            model.addAttribute("message","暂无此人");
//        }
//
//
//        Page<Student> users=studentService.getUserList(pageNum, pageSize);
//        model.addAttribute("users", users);
//
//        return "/student/list";
//        }
    @RequestMapping("/student/list")
    public String list(HttpServletRequest httprequest,Model model){
        HttpSession session = httprequest.getSession();
        if(session.getAttribute("userid") == null){
            //没有登录，需要去登录
            return "redirect:/login/user";
        }
        List<Student> students = studentService.findAll();
        model.addAttribute("data",students);
        return "student/list";
    }



//    public String list(Model model){
//
//        List<Student> students = studentService.findAll();
//        List<Student> students = studentService.findBySname("周%");
//
//        model.addAttribute("data",students);
//        model.addAttribute("data",students);
//        return "/student/list";
//    }
//
}
